package trabalho.pkg3;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

public class InfoArq
{
    private String NomeArq;
    private long Size;
    private String Hosts;
    private File Arq ; //Arquivo com as informações do arquivo
    private File dir = new File("INFO");
    
    public InfoArq(String nome)
    {
        String filepath = nome + ".obj";
        Arq = new File(dir, filepath);
    }
    
    public boolean load()
    {
        if(dir.mkdir() || dir.exists())
        {
            try
            {
                try(ObjectInputStream reader = new ObjectInputStream(new FileInputStream(Arq)))
                {
                    this.NomeArq = (String) reader.readObject();
                    this.Size = (long) reader.readObject();
                    this.Hosts = (String) reader.readObject();
                }
            } catch(ClassNotFoundException | IOException ex)
            {
                Logger.getLogger(InfoArq.class.getName()).log(Level.SEVERE, null, ex);
                return false; //Arquivo não existe ou erro ao ler o arquivo
            }
        }
        
        return true;
    }
    
    public void grava() throws IOException
    {
        if(dir.mkdir() || dir.exists())
        {
            try(ObjectOutputStream saida = new ObjectOutputStream(new FileOutputStream(Arq)))
            {
                saida.writeObject(this.getNomeArq());
                saida.writeObject(this.getSize());
                saida.writeObject(this.getHosts());
            }
        }
    }

    public void setNomeArq(String NomeArq)
    {
        this.NomeArq = NomeArq;
    }

    public void setSize(long Size)
    {
        this.Size = Size;
    }

    public void setHosts(String Hosts)
    {
        this.Hosts = Hosts;
    }

    public String getNomeArq()
    {
        return NomeArq;
    }

    public long getSize()
    {
        return Size;
    }

    public String getHosts()
    {
        return Hosts;
    }
}